package com.gmail.hasszhao.testvolley;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.gmail.hasszhao.testvolley.data.DOCategory;
import com.gmail.hasszhao.testvolley.data.list.CategoryList;
import com.gmail.hasszhao.testvolley.task.LoadCategoryListTask;
import com.gmail.hasszhao.testvolley.task.TaskHelper;

import java.util.List;

public final class CategoryListActivity extends BasicActivity implements Response.Listener<CategoryList>, Response.ErrorListener {
    public static final String ACTION_OPEN_CATEGORYLIST = "android.intent.action.CATEGORYLIST";
    public static final int LAYOUT = R.layout.listview;
    private ListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle _savedInstanceState) {
        super.onCreate(_savedInstanceState);
        setContentView(LAYOUT);
    }

    @Override
    protected void onResume() {
        showProgressIndicator();
        new LoadCategoryListTask(getApplicationContext(), Request.Method.GET, API.URL_CATEGORY_LIST, this, this).execute();
        super.onResume();
    }


    @Override
    protected void onPause() {
        TaskHelper.getRequestQueue().cancelAll(LoadCategoryListTask.TAG);
        dismissProgressIndicator();
        super.onPause();
    }



    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(getApplicationContext(), getString(R.string.err_loading_category_list), Toast.LENGTH_SHORT).show();
        dismissProgressIndicator();
    }

    @Override
    public void onResponse(CategoryList _response) {
        showData(_response);
        dismissProgressIndicator();
    }

    private void showData(CategoryList _response) {
        if (mListAdapter != null) {
            mListAdapter.setData(_response.getCategories());
            mListAdapter.notifyDataSetChanged();
        } else {
            ListView listView = (ListView) findViewById(R.id.lv_data);
            mListAdapter = new ListAdapter(getApplicationContext());
            mListAdapter.setData(_response.getCategories());
            listView.setAdapter(mListAdapter);
        }
    }

    static class ViewHolder {
        TextView displayName;

        ViewHolder(TextView _displayName) {
            displayName = _displayName;
        }
    }

    static class ListAdapter extends BaseAdapter {
        int LAYOUT = R.layout.entry_category_list;
        List<DOCategory> mData;
        Context mContext;

        ListAdapter(Context _context) {
            mContext = _context;
        }

        public void setData(List<DOCategory> _data) {
            mData = _data;
        }

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int _position) {
            return mData.get(_position);
        }

        @Override
        public long getItemId(int _position) {
            return _position;
        }

        @Override
        public View getView(int _position, View _convertView, ViewGroup _parent) {
            ViewHolder viewHolder;
            if (_convertView == null) {
                _convertView = View.inflate(mContext, LAYOUT, null);
                viewHolder = new ViewHolder((TextView) _convertView.findViewById(R.id.tv_display_name));
                _convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) _convertView.getTag();
            }
            viewHolder.displayName.setText(mData.get(_position).getEnglish_category_name());
            return _convertView;
        }
    }
}
